package com.yuven.baselib.component;

import android.content.Context;
import android.os.Bundle;

import androidx.annotation.MainThread;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import com.gyf.immersionbar.components.ImmersionFragment;
import com.yuven.baselib.component.eventbus.Event;
import com.yuven.baselib.component.eventbus.EventBusUtil;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * @author :        Yuven
 * @date :          2022/7/6 6:52 下午
 * @desc :
 */
public class FrameBaseEmptyFragment extends ImmersionFragment implements BackPressedListener {
    protected AppCompatActivity mActivity;
    protected Context mContext;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (registerEventBus()) {
            EventBusUtil.register(this);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mActivity = (AppCompatActivity) (mContext = context);
    }

    /**
     * 子类接受事件 重写[.handleEvent]
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(Event<Object> event) {
        if (event != null) {
            handleEvent(event);
        }
    }


    @Override
    public void onDestroy() {
        if (registerEventBus()) {
            EventBusUtil.unregister(this);
        }
        super.onDestroy();
    }

    /**
     * 需要接收事件 重写该方法 并返回true
     */
    protected boolean registerEventBus() {
        return false;
    }

    @MainThread
    protected void handleEvent(Event<Object> event) {
    }

    @MainThread
    protected void handleStickyEvent(Event<Object> event) {
    }

    @Override
    public boolean handleBackPressed() {
        return false;
    }

    @Override
    public void initImmersionBar() {

    }
}
